package net.maku.module.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.common.utils.TreeUtils;
import net.maku.module.asset.convert.AssetStorageConvert;
import net.maku.module.asset.entity.AssetStorageEntity;
import net.maku.module.asset.service.AssetStorageService;
import net.maku.module.asset.vo.AssetStorageVO;
import net.maku.newbase.NewBaseController;
import net.maku.newbase.NewQuery;
import net.maku.newbase.PageParam;

import net.maku.newbase.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务_资产仓库
 *
 * @author
 */
@RestController
@RequestMapping("nb/asset/storage")
@Tag(name = "业务_资产仓库")
@AllArgsConstructor
public class AssetStorageController extends NewBaseController<AssetStorageService, AssetStorageEntity> {

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<AssetStorageVO>> page(PageParam page, NewQuery query) {
        LambdaQueryWrapper<AssetStorageEntity> select = new LambdaQueryWrapper<>();

        IPage<AssetStorageEntity> iPage = entityService.page(page.b(), select);
        return Result.ok(new PageResult<>(AssetStorageConvert.INSTANCE.convertList(iPage.getRecords()), iPage.getTotal()));
    }

    @Operation(summary = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public R list(PageParam page, NewQuery query, AssetStorageEntity entity) {
        List<AssetStorageEntity> list = entityService.list();

        return R.ok(TreeUtils.build(AssetStorageConvert.INSTANCE.convertList(list), Constant.ROOT));
    }

    @PutMapping("editIsEnable")
    @Operation(summary = "更新 启用状态")
    public Result updateIsEnableById(@RequestBody AssetStorageVO vo) {
        AssetStorageEntity entity = new AssetStorageEntity();
        entity.setId(vo.getId());
        entity.setIsEnable(vo.getIsEnable());
        return Result.ok(entityService.updateIsEnableById(entity));
    }

}